import {getUserByToken} from "@/models/user";
import {AppModel} from '../models/app.js'

const appModel = new AppModel()

export const userMinix = {

    data() {

    },
    methods: {
        /**
         * 通过token 获取用户信息
         * @param token
         * @returns {Promise<*>}
         */
        async getUserByUserToken(token) {
            return await getUserByToken(token)
        },

        /**
         * 获取用户权限
         */

        doLoginLogic() {
            // console.log(this.$store.getters.userInfo,222)
            // console.log(uni.getStorageSync('AuthTokens'),111)


            return new Promise((resolve, reject) => {
                uni.login({
                    success: (res) => {
                        if (res.code) {
                            const params = {code: res.code};
                            // 调取小程序信息的接口
                            appModel
                                .checkLogin(params)
                                .then((response) => {
                                    if (response.data.data !== null) {
                                        // 如果已经存在了数据则放入缓存使用
                                        this.$store.dispatch('setUserInfo', response.data.data); // 存入vuex
                                        uni.setStorageSync('AuthTokens', response.data.data.token); // 存入缓存
                                        uni.login({
                                            success(response) {
                                                uni.setStorageSync('wxCode', response.code); // 存入缓存
                                            },
                                        });
                                        resolve();

                                    }
                                })
                                .catch((errors) => {
                                    console.log(errors);
                                    reject(errors);
                                });
                        } else {
                            console.log('获取用户登录态失败！' + res.errMsg);
                        }
                    },
                    fail: (error) => {
                        uni.showToast({title: '微信登录失败', icon: 'none'});
                        reject(error);
                    },
                });
            });

        }
    },
    onShow() {

    }
}
